<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class AdminWeb extends AdminBase
{
    public function index(){
        $param = $this->request->param();
        if(empty($param['admin_id'])) {
            $this->error('参数错误');
        }
        $data = Db::name('admin')->field('web_ids')->find($param['admin_id']);
        $list = Db::name('site_web')->where([
            'id' => ['in',$data['web_ids']]
        ])->select();
        return view('',['list'=>$list,'param'=>$param]);
    }

    public function edit(){
        $param = $this->request->param();
        if(empty($param['admin_id'])) {
            $this->error('参数错误');
        }
        if( $this->request->isPost() ) {
            $web_ids = '';
            if(!empty($param['web_ids'])){
                $web_ids = implode(',',$param['web_ids']);
            }
            $result = Db::name('admin')->where('id',$param['admin_id'])->update([
                'web_ids' => $web_ids
            ]);
            if( $result ) {
                $this->success('操作成功',url('index',['admin_id'=>$param['admin_id']]));
            } else {
                $this->error('操作失败');
            }
        }
        $data = Db::name('admin')->field('web_ids')->find($param['admin_id']);
        $web_ids = explode(',',$data['web_ids']);
        $weblist = Db::name('site_web')->select()->toArray();
        return view('form',['weblist'=>$weblist,'param'=>$param,'web_ids'=>$web_ids]);
    }

}
